var util = require('../../utils/util.js');
var ajax = require('../../utils/ajax.js');
var common = require('../../utils/common.js');
var PageBase = require('../../common/PageBase.js');
var Pagination = require('../../common/pagination/index.js');
var CutList = require('../../components/cutList/index.js');
var app = getApp();
var sliderWidth = 96; // 需要设置slider的宽度，用于计算中间位置

Page(common.extendPage(PageBase, {
	data: {
		dataLoaded: false,
		logoImg: '',

		tabs: ['活动', '砍价'],
		activeIndex: 0,
		sliderOffset: 0,
		sliderLeft: 0,
		sliderWidth,
	},

	onLoad(options) {
		common.initApp({
			extSuc: () => {
				this._getList();
				this._initTabs();
				new CutList({
                    scope: 'cutList',
                    page: this
                });
			},
			loginSuc: () => {
				this.setData({
					logoImg: app.getLogoImg()
				});
				this.$loginSuc();
			},
			sysInfoSuc: (sysInfo) => {
				this.setData({
					winHei: app.getWinHei(),
				});
			},
		});
	},

	onPullDownRefresh() {
		this._pagination && this._pagination.flush(() => {
			wx.stopPullDownRefresh();
		});
	},

	onReachBottom() {
		this._pagination && this._pagination.loadNext();
	},

	_initTabs() {
		var that = this;

		wx.getSystemInfo({
			success: function(res) {
				that.setData({
					sliderLeft: (res.windowWidth / that.data.tabs.length - sliderWidth) / 2,
					sliderOffset: res.windowWidth / that.data.tabs.length * that.data.activeIndex
				});
			}
		});
	},

	tabClick: function(e) {
		var id = e.currentTarget.id;
		var formId= e.detail.formId;

		this.setData({
			sliderOffset: e.currentTarget.offsetLeft,
			activeIndex: id
		});
		common.submitForm(formId);
	},

	/**
	 * 获取活动列表
	 * @return {[type]} [description]
	 */
	_getList() {
		this._pagination = new Pagination({
			page: this,
			scope: 'pagination'
		}).init({
			rendData: (page, cb) => {

				ajax.G({
					url: '/market/Home/getActData',
					data: {
						page,
					},
					success: (res) => {
						var {
							actList = []
						} = res.result;
						var allList = this.data.actList || [];

						this.setData({
							dataLoaded: true,
							actList: page == 1 ? actList : allList.concat(actList)
						});

						cb && cb(actList.length);
					}
				});

			}
		});
	},

}));